package org.lee.system.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.solon.service.impl.ServiceImpl;
import org.apache.ibatis.solon.annotation.Db;
import org.lee.common.core.vo.system.SysRoleVo;
import org.lee.system.mapper.SysRoleMapper;
import org.lee.system.model.SysRole;
import org.lee.system.service.SysRoleService;
import org.noear.solon.annotation.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 角色 业务层处理
 *
 * @author Lion Li
 */
@Component
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Db("lee")
    SysRoleMapper roleMapper;

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectRolePermissionByUserId(Long userId) {
        List<SysRoleVo> perms = roleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRoleVo perm : perms) {
            if (ObjectUtil.isNotNull(perm)) {
                permsSet.addAll(CharSequenceUtil.split(perm.getRoleKey().trim(), ','));
            }
        }
        return permsSet;
    }

}
